package com.oa.pm.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.joneying.common.web.response.Resp;
import com.oa.admin.entity.UserHoliday;
import com.oa.admin.service.IUserHolidayService;
import com.oa.core.PageResult;
import com.oa.core.constant.HolidaySetting;
import com.oa.core.enumm.ProcessDefineEnum;
import com.oa.core.exception.BusinessException;
import com.oa.core.exception.DataIsEmptyException;
import com.oa.pm.entity.PostponedLeaveApply;
import com.oa.pm.mapper.PostponedLeaveApplyMapper;
import com.oa.pm.service.IPostponedLeaveApplyService;
import com.oa.pm.vo.PostponedLeaveApplyListVO;
import com.oa.system.dto.ApplyApprovalAddDTO;
import com.oa.system.dto.ApprovalDTO;
import com.oa.system.entity.ApplyApproval;
import com.oa.system.entity.User;
import com.oa.system.service.IApplyApprovalService;
import com.oa.system.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class PostponedLeaveApplyServiceImpl extends ServiceImpl<PostponedLeaveApplyMapper, PostponedLeaveApply> implements IPostponedLeaveApplyService {

    @Autowired
    private PostponedLeaveApplyMapper postponedLeaveApplyMapper;

    @Autowired
    private IApplyApprovalService applyApprovalService;

    @Autowired
    private IUserService userService;

    @Autowired
    private IUserHolidayService userHolidayService;



    @Override
    public PageResult<PostponedLeaveApplyListVO> findPostponedLeaveApplyList(Integer pageNum, Integer pageSize, String userId, String name) {
        Page page = new Page(pageNum, pageSize);
        return new PageResult<>(postponedLeaveApplyMapper.findPostponedLeaveApplyList(page, userId, name), page);
    }

    @Override
    public PostponedLeaveApplyListVO findPostponedLeaveApplyDetails(String id) {
        return postponedLeaveApplyMapper.findPostponedLeaveApplyDetails(id);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Resp add(String userId, String remark) throws BusinessException, DataIsEmptyException {
        User user = userService.selectById(userId);

        UserHoliday userHoliday = userHolidayService.selectOne(new EntityWrapper().eq("holiday_id", HolidaySetting.ANNUAL_LEAVE).eq("user_id", user.getId()));

        if(userHoliday.getSurplusDay() == 0.0){
            return new Resp().failure("无可用年假时长");
        }

        PostponedLeaveApply postponedLeaveApply = new PostponedLeaveApply();
        postponedLeaveApply.setUserId(user.getId());
        postponedLeaveApply.setSurplusDay(userHoliday.getSurplusDay());
        postponedLeaveApply.setRemark(remark);

        if(insert(postponedLeaveApply)){
            ApplyApprovalAddDTO applyApprovalAddDTO = new ApplyApprovalAddDTO();
            applyApprovalAddDTO.setApplyId(postponedLeaveApply.getId());
            applyApprovalAddDTO.setApprovalProcessId(ProcessDefineEnum.POSTPONED_LEAVE.getId());
            applyApprovalAddDTO.setApplyApprovalName(user.getName() + ProcessDefineEnum.POSTPONED_LEAVE.getText());
            applyApprovalAddDTO.setApprovalUserId(user.getId());
            return applyApprovalService.addApproval(applyApprovalAddDTO);
        }

        throw new BusinessException("添加申请失败!");
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Resp audit(String userId, String id, Integer status) throws DataIsEmptyException, BusinessException {
        ApprovalDTO approvalDTO = new ApprovalDTO();
        approvalDTO.setId(Long.valueOf(id));
        approvalDTO.setUserId(userId);
        approvalDTO.setStatus(status);
        return applyApprovalService.audit(approvalDTO, d -> {
           if(d.getStatus() == 2){
               if(d.isLastStatus()){
                   PostponedLeaveApply postponedLeaveApply = selectById(d.getApplyApproval().getApplyId());
                   UserHoliday userHoliday = userHolidayService.selectOne(new EntityWrapper().eq("holiday_id", HolidaySetting.POSTOONED_LEAVE).eq("user_id", postponedLeaveApply.getUserId()));
                   userHoliday.setSurplusDay((userHoliday.getSurplusDay() == null ? 0 : userHoliday.getSurplusDay()) + postponedLeaveApply.getSurplusDay());
                   UserHoliday userHoliday2 = userHolidayService.selectOne(new EntityWrapper().eq("holiday_id", HolidaySetting.ANNUAL_LEAVE).eq("user_id", postponedLeaveApply.getUserId()));
                   userHoliday2.setSurplusDay(0.0);
                   userHolidayService.updateById(userHoliday);
                   userHolidayService.updateById(userHoliday2);
               }
           }
        });
    }
}
